package com.example.demo.controller;


import com.example.demo.dto.ApiResponse;
import com.example.demo.entity.Product;
import com.example.demo.service.ProductService;
import com.example.demo.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 商品管理控制器
 * 处理商品相关的HTTP请求，包括创建、查询、更新和删除商品等操作
 * 支持按类别、商家、价格范围等多种方式查询商品
 */
@RestController
@RequestMapping("/api/products")
public class ProductController {
    
    // 注入商品服务
    @Autowired
    private ProductService productService;
    
    /**
     * 创建新商品
     * 
     * @param product 商品信息
     * @return 创建成功的商品信息
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Product>> createProduct(@RequestBody Product product) {
        try {
            Product createdProduct = productService.createProduct(product);
            return ResponseEntity.ok(ApiResponse.success("商品创建成功", createdProduct));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    /**
     * 根据ID获取商品信息
     * 
     * @param productId 商品ID
     * @return 商品信息
     */
    @GetMapping("/{productId}")
    public ResponseEntity<ApiResponse<Product>> getProductById(@PathVariable Integer productId) {
        try {
            Product product = productService.getProductById(productId);
            return ResponseEntity.ok(ApiResponse.success(product));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    /**
     * 获取所有商品列表
     * 
     * @return 商品列表
     */
    @GetMapping("/all")
    public ResponseEntity<ApiResponse<List<Product>>> getAllProducts() {
        List<Product> products = productService.getAllProducts();
        return ResponseEntity.ok(ApiResponse.success(products));
    }
    
    /**
     * 根据类别获取商品列表
     * 
     * @param categoryId 类别ID
     * @return 指定类别的商品列表
     */
    @GetMapping("/category/{categoryId}")
    public ResponseEntity<ApiResponse<List<Product>>> getProductsByCategory(@PathVariable Integer categoryId) {
        List<Product> products = productService.getProductsByCategory(categoryId);
        return ResponseEntity.ok(ApiResponse.success(products));
    }
    
    /**
     * 根据商家获取商品列表
     * 
     * @param merchantId 商家ID
     * @return 指定商家的商品列表
     */
    @GetMapping("/merchant/{merchantId}")
    public ResponseEntity<ApiResponse<List<Product>>> getProductsByMerchant(@PathVariable Integer merchantId) {
        List<Product> products = productService.getProductsByMerchant(merchantId);
        return ResponseEntity.ok(ApiResponse.success(products));
    }
    
    /**
     * 获取宠物或用品列表
     * 

     * @return 宠物列表
     */
    @GetMapping("/pets/1")
    public ResponseEntity<ApiResponse<List<Product>>> getPetProducts() {
        List<Product> products = productService.getPetProducts(true);
        return ResponseEntity.ok(ApiResponse.success(products));
    }

    /**
     * 获取宠物或用品列表
     *

     * @return 用品列表
     */
    @GetMapping("/pets/2")
    public ResponseEntity<ApiResponse<List<Product>>> getPetProducts1() {
        List<Product> products = productService.getPetProducts(false);
        return ResponseEntity.ok(ApiResponse.success(products));
    }

    /**
     * 搜索商品
     * 
     * @param keyword 搜索关键词
     * @return 匹配的商品列表
     */
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<Product>>> searchProducts(@RequestParam String keyword) {
        List<Product> products = productService.searchProducts(keyword);
        return ResponseEntity.ok(ApiResponse.success(products));
    }
    
    /**
     * 根据价格范围获取商品列表
     * 
     * @param minPrice 最低价格
     * @param maxPrice 最高价格
     * @return 价格在指定范围内的商品列表
     */
    @GetMapping("/price-range")
    public ResponseEntity<ApiResponse<List<Product>>> getProductsByPriceRange(
            @RequestParam BigDecimal minPrice, @RequestParam BigDecimal maxPrice) {
        List<Product> products = productService.getProductsByPriceRange(minPrice, maxPrice);
        return ResponseEntity.ok(ApiResponse.success(products));
    }
    
    /**
     * 获取可用商品列表
     * 
     * @return 可用的商品列表
     */
    @GetMapping("/available")
    public ResponseEntity<ApiResponse<List<Product>>> getAvailableProducts() {
        List<Product> products = productService.getAvailableProducts();
        return ResponseEntity.ok(ApiResponse.success(products));
    }
    
    /**
     * 更新商品信息
     * 
     * @param productId 商品ID
     * @param product 更新的商品信息
     * @return 更新后的商品信息
     */
    @PutMapping("/{productId}")
    public ResponseEntity<ApiResponse<Product>> updateProduct(@PathVariable Integer productId, @RequestBody Product product) {
        try {
            product.setProductId(productId);
            Product updatedProduct = productService.updateProduct(product);
            return ResponseEntity.ok(ApiResponse.success("商品更新成功", updatedProduct));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    /**
     * 删除商品
     * 
     * @param productId 商品ID
     * @return 操作结果
     */
    @DeleteMapping("/{productId}")
    public ResponseEntity<ApiResponse<Void>> deleteProduct(@PathVariable Integer productId) {
        try {
            productService.deleteProduct(productId);
            return ResponseEntity.ok(ApiResponse.success("商品删除成功", null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    /**
     * 分页查询商品列表
     * 
     * @param current 当前页码
     * @param size 每页大小
     * @return 分页商品列表
     */
    @GetMapping("/page")
    public ResponseEntity<ApiResponse<IPage<Product>>> getProductPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        // 创建分页对象
        Page<Product> page = new Page<>(current, size);
        // 执行分页查询
        IPage<Product> productPage = productService.page(page);
        return ResponseEntity.ok(ApiResponse.success(productPage));
    }
}